package com.zbbt.sportslottery.net;

import java.io.File;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.app.ServerConfig;
import com.zbbt.sportslottery.util.LogUtil;
import com.zbbt.sportslottery.util.Md5;

public class ZbHttpClient {
	private static String TAG = Constant.HTTP_TAG;
	private static HttpUtils httpUtils;
	static {
		httpUtils = new HttpUtils();
	}

	public static void download(String url, String path, RequestCallBack<File> callBack) {
		LogUtil.printI(TAG, url);
		httpUtils.download(url, path, callBack);
	}

	public static <T> void postNewApi(String func, JsonObject jsonObject, RequestCallBack<T> callBack) {
		String url = ServerConfig.SERVER_URL + func;
		LogUtil.printI(TAG, url);
		httpUtils.send(HttpMethod.POST, url, getRequestData(jsonObject), callBack);
	}

	public static <T> void getNewApi(String func, JsonObject jsonObject, RequestCallBack<T> callBack) {
		String url = ServerConfig.SERVER_URL + func;
		LogUtil.printI(TAG, url);
		httpUtils.send(HttpMethod.GET, url, getRequestData(jsonObject), callBack);
	}

	public static <T> void postImg(String func, String fileString, RequestParams params,
			RequestCallBack<T> callBack) {
		String url = ServerConfig.SERVER_URL + func;
		LogUtil.printI(TAG, url);
		params.addQueryStringParameter("brand", App.PARAM_BRAND);// 手机品牌
		params.addQueryStringParameter("network", App.PARAM_NETWORK);// 网络
		params.addQueryStringParameter("sysVer", App.PARAM_SYSVER);// 系统版本
		params.addQueryStringParameter("softVer", App.PARAM_SOFTVER);// App版本
		params.addQueryStringParameter("platform", App.PARAM_PLATFORM);// 平台
		params.addQueryStringParameter("deviceId", App.PARAM_DEVICEID);// 设备号
		params.addQueryStringParameter("appId", App.PARAM_APPID);// App区域
		params.addQueryStringParameter("sid", App.PARAM_SID);// 渠道号
		params.addQueryStringParameter("appNumber", App.APP_NUMBER);
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append(App.PARAM_DEVICEID)//
				.append(App.PARAM_APPID)//
				.append(App.PARAM_PLATFORM)//
				.append(App.PARAM_SOFTVER)//
				.append(Constant.SECURITY_KEY);// 加密key
		String md5Str = Md5.getMD5(sBuilder.toString());
		params.addQueryStringParameter("sign", md5Str);// md5
		params.addBodyParameter("img", new File(fileString));
		httpUtils.send(HttpMethod.POST, url, params, callBack);
	}

	private static RequestParams getRequestData(JsonObject jsonObject) {
		if (jsonObject == null) {
			jsonObject = new JsonObject();
		}
		jsonObject.addProperty("brand", App.PARAM_BRAND);// 手机品牌
		jsonObject.addProperty("network", App.PARAM_NETWORK);// 网络
		jsonObject.addProperty("sysVer", App.PARAM_SYSVER);// 系统版本
		jsonObject.addProperty("softVer", App.PARAM_SOFTVER);// App版本
		jsonObject.addProperty("platform", App.PARAM_PLATFORM);// 平台
		jsonObject.addProperty("deviceId", App.PARAM_DEVICEID);// 设备号
		jsonObject.addProperty("appId", App.PARAM_APPID);// App区域
		jsonObject.addProperty("sid", App.PARAM_SID);// 渠道号
		jsonObject.addProperty("appNumber", App.APP_NUMBER);
		jsonObject.addProperty("category", App.PARAM_CATEGORY);//标识体彩与福彩
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append(App.PARAM_DEVICEID)//
				.append(App.PARAM_APPID)//
				.append(App.PARAM_PLATFORM)//
				.append(App.PARAM_SOFTVER)//
				.append(Constant.SECURITY_KEY);// 加密key
		String md5Str = Md5.getMD5(sBuilder.toString());
		jsonObject.addProperty("sign", md5Str);// md5
		RequestParams params = new RequestParams();
		Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
		Iterator<Entry<String, JsonElement>> iterator = entrySet.iterator();
		while (iterator.hasNext()) {
			Entry<String, JsonElement> next = iterator.next();
			if (next.getValue().isJsonNull()) {
				params.addQueryStringParameter(next.getKey(), "");
			} else {
				params.addQueryStringParameter(next.getKey(), next.getValue().getAsString());

			}
		}
		LogUtil.printI(TAG, "jsonObject:->" + jsonObject.toString());
		return params;
	}
}
